package cn.ztydata.teachingsystem.heaven.dao.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * 作业提交计数
 *
 * @author 陈鑫
 * @since 2015-03-19
 */
@Repository
public class TestSubmittedCounterDao {
    protected static Logger logger = LoggerFactory.getLogger(TestSubmittedCounterDao.class); //日志记录器

    @Resource(name = "stringRedisTemplate")
    protected RedisTemplate redisTemplate; //sdr-Redis模版

    private static final String TEST_SUBMITTED = "test:submitted";

    /**
     * 递增作业提交数
     * @param testId 作业测试ID
     * @return long
     *
     * @author cx
     * @since 2015-03-19
     */
    public long incrSubmittedNum(final long testId){
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();

        logger.debug("递增作业提交数");
        return valueOperations.increment(KeyUtils.generateKey(TEST_SUBMITTED, testId), 1);
    }

    /**
     * 获取作业提交数
     * @param testId 作业ID
     * @return Long
     *
     * @author cx
     * @since 2015-03-19
     */
    public long getSubmittedNum(final long testId){
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();

        String num = valueOperations.get(KeyUtils.generateKey(TEST_SUBMITTED, testId));

        if(null == num){
            return 0;
        }

        return Long.parseLong(num);
    }
}
